package cn.wolfcode.luowowo.mgrsite.job;

import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.article.vo.StrategyStatisPersistenceVO;
import cn.wolfcode.luowowo.article.vo.TravelStatisPersistenceVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.vo.TravelStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * 定时器, 定时执行redis数据持久化
 */
//@Component
public class RedisDataPersistenceJob {

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelService travelService;

    @Reference
    private ITravelStatisRedisService travelStatisRedisService;


    //Cron表达式是一个字符串，字符串以5或6个空格隔开，分为6或7个域，每一个域代表一个含义

    //Seconds Minutes Hours DayofMonth Month DayofWeek   Year
    //  秒     分     时        日        月     周几       年
    //Seconds Minutes Hours DayofMonth Month DayofWeek      [spring支持]

    //  0      0       2       1        *      ?          *   表示在每月的1日的凌晨2点调整任务
    //  0     15      10       ?        *    MON-FRI          表示周一到周五每天上午10:15执行作业



    //表示给方法为定时任务方法
    //cron 属性: 表示任务计划   定时策略
    @Scheduled(cron = "0/10 * * * * ? ")
    public void dosomething(){
        System.out.println("---------------vo(攻略)持久化--begin-----------------------");
        List<StrategyStatisVO> list = strategyStatisRedisService.getVOsByPattern(RedisKeys.STRATEGY_STATIS_VO.join("*"));

        StrategyStatisPersistenceVO pvo = null;
        for (StrategyStatisVO vo : list) {
            pvo = new StrategyStatisPersistenceVO();
            try {
                BeanUtils.copyProperties(pvo,vo);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            //将vo对象update 进mysql数据库中
            strategyDetailService.updateStatisVO(pvo);

        }




        System.out.println("---------------vo(攻略)持久化--end-----------------------");

        System.out.println("---------------vo(游记)持久化--begin-----------------------");
        List<TravelStatisVO> list1 = travelStatisRedisService.getVOsByPattern(RedisKeys.TRAVEL_STATIS_VO.join("*"));

        TravelStatisPersistenceVO pvo1 = null;
        for (TravelStatisVO vo1 : list1) {
            pvo1 = new TravelStatisPersistenceVO();
            try {
                BeanUtils.copyProperties(pvo1,vo1);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            //将vo对象update 进mysql数据库中
            travelService.updateStatisVO(pvo1);

        }

        System.out.println("---------------vo(游记)持久化--end-----------------------");
    }
}
